import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3

Item {
    id:root;
    property var background: "#d7e3bc";
    property var alterBackground: "white";
    property var highlight: "blue";
    property var headerBackground: "#F0F0F0";
    property var records;

    function updateDatas() {
        tableView.updateDatas();
    }

    TableView
    {
        id: tableView
        anchors.fill: parent
        anchors.margins: 1
        model: records
        selectionMode: SelectionMode.NoSelection
        //点击过cell后，记录cell的column
        property int currentColumn: -1

        TableViewColumn { id: idColumn; role: "number"; title: qsTr("序号"); width:35;
            horizontalAlignment: Text.AlignHCenter; resizable: false; movable: false; }
        TableViewColumn { id: checkedColumn; role: "checked"; title: qsTr("选择"); width:40;
            horizontalAlignment: Text.AlignHCenter; resizable: false; movable: false;}
        TableViewColumn { role: "name"; title: qsTr("编号"); width: 400; }
        TableViewColumn { role: "checkTime"; title: qsTr("测试日期");  }

        rowDelegate: Rectangle{
            height: 22;
            color: styleData.selected ? root.highlight : (styleData.alternate ? root.alterBackground : root.background);
        }

        itemDelegate: itemDelegate

        onSortIndicatorColumnChanged: {
            if(1===sortIndicatorColumn ){
                console.log("header clicked");
                records.checkedAllSame();
            }
        }
    }

    //TabelView item代理
    Component {
        id: itemDelegate
        //Loader 动态加载不同的组件
        Loader {
            id: itemLoader
            anchors.fill: parent

            visible: status === Loader.Ready
            //根据role加载相应的组件

            sourceComponent: {
                var role = styleData.role;
                if(role === "number")
                    return idComponent;
                else if (role === "checked")
                    return checkComponent;
                else if (role === "name")
                    return nameComponent;
                else return emptyComponent;
            }
            Component {
                id: emptyComponent
                Text
                {
                    anchors.left: parent.left
                    anchors.verticalCenter: parent.verticalCenter
                    text: styleData.value
                    verticalAlignment: Text.AlignVCenter
                }
            }
            Component {
                id: idComponent
                Text
                {
                    anchors.centerIn: parent
                    text: styleData.value
                    verticalAlignment: Text.AlignVCenter
                }
            }
            Component {
                id: checkComponent
                CheckBox{
                    anchors.centerIn: parent
                    checked: styleData.value
                    onCheckedChanged: {
                        tableView.currentColumn = styleData.column;
                        parent.forceActiveFocus();
//                        if (tableView.controlKeyPressed && styleData.row >= 0 ) {
//                            if (styleData.selected) {
//                                tableView.selection.deselect(styleData.row);
//                            } else {
//                                tableView.selection.select(styleData.row);
//                            }
//                        }
//                        else {
//                            tableView.selection.clear();
//                            tableView.selection.select(styleData.row)
//                        }
                        records.setProperty(styleData.row,checked, styleData.role);
                    }
                }
            }

            Component {
                id: nameComponent
                Text
                {
                    anchors.left: parent.left
                    anchors.verticalCenter: parent.verticalCenter
                    text: styleData.value
                    verticalAlignment: Text.AlignVCenter
                }
            }
        }
    }
}
